package com.zh.work.util.plateform;

import com.zh.work.entity.agent.AgentInfoEntity;
import com.zh.work.entity.branch.BranchInfoEntity;
import com.zh.work.entity.merch.MerchInfoEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;


/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 构造机构及下属权限树
 * @Author: Liard
 * @Create in: 2020/6/8 9:27
 * @Version: 1.0.1
 */
public class TreeBranchUtil {
    private static final Logger log = LoggerFactory.getLogger(TreeBranchUtil.class);
    private List<BranchInfoEntity> branchInfoList;
    private List<AgentInfoEntity> agentInfoList;
    private List<MerchInfoEntity> merchInfoList;


    public static TreeBranchUtil treeBranchUtil = new TreeBranchUtil();

    public static TreeBranchUtil getInstance() { return treeBranchUtil; }

    public Map<String, Object> getBranchTreeData(Map<String, Object> hashMap) {

        // 获取机构列表
        this.branchInfoList = (List<BranchInfoEntity>) hashMap.get("branch");

        // 获取代理商列表
        this.agentInfoList = (List<AgentInfoEntity>) hashMap.get("agent");

        // 获取商户列表
        this.merchInfoList = (List<MerchInfoEntity>) hashMap.get("merchant");

        getBranchList();
        Map<String, Object> map = new HashMap<>();
        map.put("nodes", branchInfoList);
        return map;
    }

    // 解析出机构下属代理集合
    private void getBranchList() {
        for (int i = 0; i < branchInfoList.size(); i++) {
            branchInfoList.get(i).setChildren(getAgentList(branchInfoList.get(i)));
            if (branchInfoList.get(i).getChildren().size() <= 0)
                branchInfoList.get(i).setIsLeaf(true);
        }
    }

    // 获取代理列表
    private List<AgentInfoEntity> getAgentList(BranchInfoEntity node){
        List<AgentInfoEntity> list = new LinkedList<>();
        for (int i = 0; i < agentInfoList.size(); i++) {
            agentInfoList.get(i).setChildren(getMerchList(agentInfoList.get(i)));
            if (agentInfoList.get(i).getBranchNo().equals(node.getValue())) {
                list.add(agentInfoList.get(i));
                if (agentInfoList.get(i).getChildren().size()  <= 0) {
                    agentInfoList.get(i).setIsLeaf(true);
                }
            }
        }
        return list;
    }

    // 获取商户列表
    private List<MerchInfoEntity> getMerchList(AgentInfoEntity node) {
        List<MerchInfoEntity> list = new LinkedList<>();
        for (int i = 0; i < merchInfoList.size(); i++) {
            if (Objects.isNull(merchInfoList.get(i).getSuperAgent())) {
                continue;
            }
            if (merchInfoList.get(i).getSuperAgent().equals(node.getValue())) {
                list.add(merchInfoList.get(i));
                merchInfoList.get(i).setIsLeaf(true);
            }
        }
        return list;
    }



}
